package com.nf.sms.utils;

import java.sql.*;

/**
 * @AUTHOR CJ
 * <p>
 * *@DATE 2024 - 10 - 15
 */
public class DbUtil {
    /* 账号 */
    private static final String URL = "jdbc:sqlserver://localhost:1433;databaseName=User_Information;encrypt=true;trustServerCertificate=true";
    // 驱动程序
    private static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    // 用户名
    private static final String USER = "sa";
    // 密码
    private static final String PASS = "123456";

    static {
        try {
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public static Connection getConnection() {
        try {
            return DriverManager.getConnection(URL,USER,PASS);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    /*
     *关闭相关的对象-释放资源
     * rst 结果集对象:用来存储查询的数据
     * stat:语句对象 - 用于执行 SQL 语句 Statement PreparedStutement Call
     * conn 链接对象 - 建立 Java语言与数据库桥梁
     * */
    public static void close(ResultSet rst,Statement stmt,Connection conn){
        //保证代码的健壮性
        if(rst != null){
            try {
                rst.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if(stmt!=null){
            try {
                stmt.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
